//
//  SettingViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/2.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "AppDelegate.h"
#import "UpdatePasswordViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * nameArray;
@property (nonatomic, weak)UIButton * logoutButton;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.nameArray = @[@"密码修改",@"消息通知",@"帮助",@"关于我们"];
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"设置";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xe8e8e8);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"SettingListCell"];

    [self.view addSubview:self.tableView];
    
    
    UIButton * logoutButton = [UIButton new];
    
    [logoutButton setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton setBackgroundColor:MAINCOLOR];
    [logoutButton addTarget:self action:@selector(clickLogoutButton) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    logoutButton.layer.cornerRadius = 5.0f;
    
    self.logoutButton = logoutButton;
    
    [self.view addSubview:self.logoutButton];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-40 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(230 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingListCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.nameArray[indexPath.row];
    
    if (indexPath.row == 1) {
        
        cell.switchView.hidden = NO;
        cell.rightImageView.hidden = YES;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    if (indexPath.row == 0) {
        
        UpdatePasswordViewController * updateVC = [UpdatePasswordViewController new];
        
        [self.navigationController pushViewController:updateVC animated:YES];
    }
    
    SHOW_TAB;
}


- (void)clickLogoutButton{
    
    NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dictionary = [defatluts dictionaryRepresentation];
    
    for(NSString *key in [dictionary allKeys]){
        
        [defatluts removeObjectForKey:key];
        [defatluts synchronize];
    }
    
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    MainViewController * navVC = [[MainViewController alloc] initWithRootViewController:[LoginViewController new]];
    
    appDelegate.window.rootViewController = navVC;
    
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
